﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

namespace TooltracePC
{
    class clsDatabase
    {
        private static string _strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = database\db.mdb"; //database connection info
        private OleDbConnection _oleDbConn;//Access connection object
        //private OleDbDataAdapter _oleDbAda;

        private bool openDb()
        {
            try
            {
                _oleDbConn = new OleDbConnection(_strConn);
                _oleDbConn.Open();
                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }


        private bool closeDb()
        {
            try
            {
                _oleDbConn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public DataTable SelectToDataTable(string strSql)
        {
            if (!openDb())
            {
                return null;
            }
            try
            {
                if(string.IsNullOrEmpty(strSql))
                {
                    return null;
                }
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                OleDbCommand command = new OleDbCommand(strSql, _oleDbConn);
                adapter.SelectCommand = command;
                DataTable Dt = new DataTable();
                adapter.Fill(Dt);
                closeDb();
                return Dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

        }


        public OleDbDataReader sqlSearch(string strSql)
        {
            if (!openDb())
            {
                return null;
            }
            try
            {
                if (string.IsNullOrEmpty(strSql))
                {
                    return null;
                }
                OleDbCommand _oleDbCmd = new OleDbCommand(strSql, _oleDbConn);
                OleDbDataReader _oleDtReader = _oleDbCmd.ExecuteReader();
                closeDb();
                return _oleDtReader;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

        }


        //
        public void execNonSQL(string strSQL)
        {
            if (!openDb())
            {
                return;
            }
            try
            {
                if (string.IsNullOrEmpty(strSQL))
                {
                    return;
                }

                OleDbCommand comm = new OleDbCommand(strSQL, _oleDbConn);
                comm.ExecuteNonQuery();
                closeDb();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

        }

    }
}
